package com.mxy.myapplication2.kotlin

import android.content.DialogInterface
import android.support.v7.app.AlertDialog
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.mxy.myapplication2.R
import com.mxy.myapplication2.java.Util

/**
 * kotlin的RecyclerView适配器
 */
class AdapterForKotlinRv constructor(var context: MyFirstKotlinActivity, var arrayList: ArrayList<Int>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    //kotlin数组初始化与java不同
    private var intArray: Array<Int> = arrayOf(1, 2, 3)
    private var longArray: Array<Long> = arrayOf(1, 2, 3)
    private var floatArray: Array<Float> = arrayOf(1.0f, 2.0f, 3.0f)
    private var doubleArray: Array<Double> = arrayOf(1.0, 2.0, 3.0)
    private var booleanArray: Array<Boolean> = arrayOf(true, false, true)
    private var charArray: Array<Char> = arrayOf('a', 'b', 'c')
    private val dataArray: Array<String> = arrayOf("第一个", "第二个", "第三个", "第四个", "第五个")

    private val layoutInfilate: LayoutInflater;

    init {
        this.layoutInfilate = LayoutInflater.from(this.context);
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return MyViewHolder(this.layoutInfilate.inflate(R.layout.item_kotlinactivityrv, null, false));
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val myViewHolder: MyViewHolder = holder as MyViewHolder
        myViewHolder.textView.setOnClickListener(OnItemClickListener(position))
    }


    override fun getItemCount(): Int {
        return this.arrayList.size
    }

    /**
     * 自定义ViewHolder
     * */
    inner class MyViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val textView: TextView;

        init {
            textView = itemView.findViewById<TextView>(R.id.textView)
        }
    }

    /**
     * item点击事件
     * @param position 点击的列表item序列
     * */
    inner class OnItemClickListener constructor(val position: Int) : View.OnClickListener {


        override fun onClick(v: View?) {
            when (this.position) {
                0 -> {
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setTitle("简单对话框")
                    alertDialog.setMessage("这是一个简单对话框")
                    alertDialog.setPositiveButton("确定", null)
                    alertDialog.show()
                }
                1 -> {

                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setTitle("带“是”和“否”的对话框")
                    alertDialog.setMessage("带“是”和“否”的对话框")
                    alertDialog.setPositiveButton("确定", null)
                    alertDialog.setNegativeButton("取消", null)
                    alertDialog.show()

                }
                2 -> {
                    val editText: EditText = EditText(context)
                    editText.maxLines = 2
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setView(editText)
                    alertDialog.setTitle("带编辑框的对话框")
                    alertDialog.setMessage("带编辑框的对话框")
                    alertDialog.setPositiveButton("确定", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            Toast.makeText(context, editText.text.toString().trim(), Toast.LENGTH_SHORT).show();
                        }
                    })
                    alertDialog.setNegativeButton("取消", null)
                    alertDialog.show()

                }
                3 -> {
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setTitle("单选对话框")
                    //注:setMessage不能和单选或多选效果同时出现
//                    alertDialog.setMessage("单选对话框")
                    //第一个参数为要加载的数据，第二个参数为默认选中的按钮，如果为0则代表第一个项目被选中，为-1则代表没有项目被选中。第三个参数可为单选列表的项目设置监听事件
                    alertDialog.setSingleChoiceItems(dataArray, -1, object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            Util.showToast(context, dataArray[which])
                        }
                    })
                    alertDialog.setPositiveButton("确定", null)
                    alertDialog.setNegativeButton("取消", null)
                    alertDialog.show()
                }
                4 -> {
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setTitle("多选对话框")
                    //注:setMessage不能和单选或多选效果同时出现
//                    alertDialog.setMessage("单选对话框")
                    //第一个参数为要加载的数据，第二个参数为默认选中项格式:[true,false,true]，null表没有项目被选中。第三个参数可为单选列表的项目设置监听事件
                    alertDialog.setMultiChoiceItems(dataArray, null, object : DialogInterface.OnMultiChoiceClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int, isChecked: Boolean) {
                            if (isChecked) {
                                Util.showToast(context, dataArray[which] + "选中")
                            }
                        }
                    })
                    alertDialog.setPositiveButton("确定", object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            Util.showToast(context, dataArray[which])
                        }
                    })
                    alertDialog.setNegativeButton("取消", null)
                    alertDialog.show()
                }
                5 -> {
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setTitle("列表对话框")
                    //注:setMessage不能和单选或多选效果同时出现
//                    alertDialog.setMessage("单选对话框")
                    alertDialog.setItems(dataArray, object : DialogInterface.OnClickListener {
                        override fun onClick(dialog: DialogInterface?, which: Int) {
                            Util.showToast(context, dataArray[which])
                            dialog?.let { it.dismiss() }
                        }
                    })
                    alertDialog.show()
                }
                6 -> {
                    val imageView: ImageView = ImageView(context)
                    imageView.scaleType = ImageView.ScaleType.CENTER_CROP;
                    imageView.setImageDrawable(context.resources.getDrawable(R.drawable.coco1))
                    val alertDialog: AlertDialog.Builder = AlertDialog.Builder(context)
                    alertDialog.setView(imageView)
                    alertDialog.setTitle("带图片的对话框")
                    alertDialog.setMessage("带图片的对话框")
                    alertDialog.setPositiveButton("确定", null)
                    alertDialog.setNegativeButton("取消", null)
                    alertDialog.show()
                }
                else -> {//相当于default

                }
            }
        }
    }
}